package edu.indiana.d2i.util.hector;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.util.ToolRunner;
import org.apache.mahout.common.AbstractJob;

public class HTRC2Text extends AbstractJob {
	private final String HTRC_DIR = "./htrc-dir";
	
	@Override
	public int run(String[] args) throws Exception {
		String keylistFile = args[0];
		BufferedReader reader = new BufferedReader(new FileReader(keylistFile));
		String volume = null;
		List<String> keyArray = new ArrayList<String>();
		while ((volume = reader.readLine()) != null) {
			if (!volume.equals("")) keyArray.add(volume.trim());
		}
		
		new File(HTRC_DIR).mkdir();
		HTRCClient client = new HTRCClient();
		client.setMaxGrabSize(10).buildClient(keyArray);
		Map<String, String> contents = client.readVolumeContents();
		while (!contents.isEmpty()) {
			Iterator<String> iterator = contents.keySet().iterator();
			while (iterator.hasNext()) {
				String id = iterator.next();
				FileWriter writer = new FileWriter(HTRC_DIR + "/" + id.substring(id.length()-9));
				writer.write(contents.get(id));
				writer.close();
			}
			contents = client.readVolumeContents();
		}

		reader.close();
		client.close();
		return 0;
	}

	public static void main(String[] args) throws Exception {
		ToolRunner.run(new HTRC2Text(), args);
	}
}
